package lumis.service.rss;

import org.w3c.dom.Node;

import lumis.doui.source.ISourceData;
import lumis.doui.source.Source;
import lumis.doui.source.TabularData;
import lumis.doui.source.postloadprocessor.IPostLoadProcessor;
import lumis.portal.PortalContext;
import lumis.portal.PortalException;
import lumis.portal.authentication.SessionConfig;
import lumis.util.ITransaction;

/**
 * Responsible for setting the href value for each rss.
 * 
 * @version $Revision: 8618 $ $Date: 2007-12-18 11:34:20 -0200 (Tue, 18 Dec 2007) $
 * @since 4.1.0
 */
public class RssPostLoadProcessor implements IPostLoadProcessor
{
	public void processSource(SessionConfig sessionConfig, Source source, Node parametersNode, ITransaction transaction) throws PortalException
	{
		String serviceInstanceId = source.getDouiContext().getRequest().getServiceInstanceConfig().getId();
		
		TabularData tabularData = (TabularData) source.getData();
		for (ISourceData row : tabularData.getRows())
		{
			String id = row.get("id", String.class);
			String relativePath = "/data/rss/" + serviceInstanceId + "/" + id + ".xml";
			
			RssConfig rssConfig = RssManagerFactory.getRssManager().get(sessionConfig, id, transaction);
			
			if (rssConfig.getPath() != null && rssConfig.getPath().length() > 0)
				relativePath = "/data/rss/" + rssConfig.getPath();

			row.put("href", PortalContext.getFrameworkUrl()+relativePath);
		}
	}
}
